unit FindAccount;

interface

uses
  Winapi.Windows, Winapi.Messages, SysUtils, Variants, Classes, Graphics,
  Controls, Forms, Dialogs, uniGUITypes, uniGUIAbstractClasses,
  uniGUIClasses, uniGUIForm, uniBasicGrid, uniDBGrid, DB, uniButton, uniBitBtn,
  uniEdit, uniLabel, uniPanel, uniGUIBaseClasses, uniStatusBar, DBClient,
  ClientRowSet, Clipbrd;

type
  TFrmFindAccount = class(TUniForm)
    UniPanel1: TUniPanel;
    UniLabel1: TUniLabel;
    edCondition: TUniEdit;
    btFind: TUniBitBtn;
    btConfirm: TUniBitBtn;
    dsAccount: TDataSource;
    UniDBGrid1: TUniDBGrid;
    UniBitBtn1: TUniBitBtn;
    procedure btFindClick(Sender: TObject);
    procedure btConfirmClick(Sender: TObject);
    procedure UniFormShow(Sender: TObject);
    procedure UniBitBtn1Click(Sender: TObject);
    procedure UniDBGrid1ColumnSort(Column: TUniDBGridColumn;
      Direction: Boolean);
    procedure UniDBGrid1CellClick(Column: TUniDBGridColumn);
    procedure UniDBGrid1DblClick(Sender: TObject);
    procedure UniFormCreate(Sender: TObject);
  private
    { Private declarations }
    currField: TField;
    Dataset:TClientRowset;
  public
    { Public declarations }
    procedure SetDataset(fdata:TClientRowset);
  end;

function FrmFindAccount: TFrmFindAccount;

implementation

{$R *.dfm}

uses
  MainModule, uniGUIApplication,AccModule, uCommFunction;

function FrmFindAccount: TFrmFindAccount;
begin
  Result := TFrmFindAccount(UniMainModule.GetFormInstance(TFrmFindAccount));
end;

procedure TFrmFindAccount.btConfirmClick(Sender: TObject);
var
  TableName:String;
begin
  if dsAccount.DataSet.RecordCount = 0 then
  begin
    exit;
  end;

  TableName := Uppercase(self.Dataset.TableName);
  self.Dataset.Edit;
  if (TableName = 'ACC_RECEIVE') or (TableName = 'ACC_PAID') then
  begin
    self.Dataset.FieldByName('COMP_NO').AsString := dsAccount.DataSet.FieldByName('ACC_NO').AsString;
    self.Dataset.FieldByName('TEL').AsString := dsAccount.DataSet.FieldByName('TEL').AsString;
    self.Dataset.FieldByName('COMPANY').AsString := dsAccount.DataSet.FieldByName('COMP_ABB').AsString;
    self.Dataset.FieldByName('CONTACTOR').AsString := dsAccount.DataSet.FieldByName('CONTACTOR').AsString;
  end;
  if (TableName = 'TSP_UNION_COMP') then
  begin
    self.Dataset.FieldByName('ACCOUNT_NO').AsString := dsAccount.DataSet.FieldByName('ACC_NO').AsString;
    self.Dataset.FieldByName('TEL').AsString := dsAccount.DataSet.FieldByName('TEL').AsString;
  end;
  if (TableName = 'TSP_UNION_COMP') then
  begin
    self.Dataset.FieldByName('ACCOUNT_NO').AsString := dsAccount.DataSet.FieldByName('ACC_NO').AsString;
    self.Dataset.FieldByName('TEL').AsString := dsAccount.DataSet.FieldByName('TEL').AsString;
  end;
  if (TableName = 'BAS_CONTACTOR') then
  begin
    self.Dataset.FieldByName('COMP_NO').AsString := dsAccount.DataSet.FieldByName('ACC_NO').AsString;
    self.Dataset.FieldByName('COMPANY').AsString := dsAccount.DataSet.FieldByName('COMP_ABB').AsString;
    self.Dataset.FieldByName('TEL').AsString := dsAccount.DataSet.FieldByName('TEL').AsString;
  end;
  if (TableName = 'ACC_TRANSFER') then
  begin
    self.Dataset.FieldByName('CONTACTOR').AsString := dsAccount.DataSet.FieldByName('CONTACTOR').AsString;
    self.Dataset.FieldByName('COMPANY').AsString := dsAccount.DataSet.FieldByName('COMP_NAME').AsString;
    self.Dataset.FieldByName('PHONENO').AsString := dsAccount.DataSet.FieldByName('TEL').AsString;
    self.Dataset.FieldByName('ACCOUNT_NO').AsString := dsAccount.DataSet.FieldByName('ACC_NO').AsString;
    self.Dataset.FieldByName('BANK').AsString := dsAccount.DataSet.FieldByName('BANK').AsString;
    self.Dataset.FieldByName('BANK_ACCOUNT').AsString := dsAccount.DataSet.FieldByName('BANK_CARDNO').AsString;
    self.Dataset.FieldByName('BANK_ADDR').AsString := dsAccount.DataSet.FieldByName('BANK_ADDR').AsString;
  end;

  self.Close;
end;

procedure TFrmFindAccount.btFindClick(Sender: TObject);
var
  condition:String;
begin
  if edCondition.Text <> '' then
    condition := '(acc_no=' + quotedstr(edCondition.Text) + ' or ' +
                 ' comp_abb like ' + quotedstr(edCondition.Text+'%') + ')';
  dmacc.ShowAccount(condition);

end;

procedure TFrmFindAccount.SetDataset(fdata: TClientRowset);
begin
  self.Dataset := fdata;
end;

procedure TFrmFindAccount.UniBitBtn1Click(Sender: TObject);
begin
  self.Close;
end;

procedure TFrmFindAccount.UniDBGrid1CellClick(Column: TUniDBGridColumn);
begin
  currField := Column.Field;
end;

procedure TFrmFindAccount.UniDBGrid1ColumnSort(Column: TUniDBGridColumn;
  Direction: Boolean);
begin
  SortColumn((UniDBGrid1.DataSource.DataSet as TClientRowSet), Column.FieldName, Direction);
end;

procedure TFrmFindAccount.UniDBGrid1DblClick(Sender: TObject);
begin
  if currField <> nil then
    Clipboard.AsText := currField.AsString;
end;

procedure TFrmFindAccount.UniFormCreate(Sender: TObject);
begin
  SetCompoentLanguage(Self);
end;

procedure TFrmFindAccount.UniFormShow(Sender: TObject);
begin
  if not dmacc.crsAccount.Active then
    dmacc.ShowAccount('');
  SetFieldsSort(UniDBGrid1);
  currField := nil;
end;

end.
